<?php
/**
 * ClassName:运费计算控制器
 * @auditor:leijunxiang
 * @time 2025/4/15 16:34
 */

namespace app\controller\freight;

use app\BaseController;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\tool\freight\FreightCalculateHandle;

class FreightCalculateController extends BaseController
{
    /**
     * 运费计算
     * @return void
     */
    public function calculate()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestObj = app(RequestAgreement::class);
        $content = $requestObj->getContent();
        //货代数组
        $attr = $content['forwardAttr'];
        if (empty($attr)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请选择货代公司！");
            return;
        }
        $areaName = isset($content['areaName']) ? $content['areaName'] : '';
        if (empty($areaName)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入仓库名称！");
            return;
        }
        $weight = isset($content['weight']) ? $content['weight'] : '';
        if (empty($weight)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入重量名称！");
            return;
        }
        $num = isset($content['num']) ? $content['num'] : '';
        if (empty($num)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入数量名称！");
            return;
        }
        $handle = app(FreightCalculateHandle::class);
        //(数组)开始计算(货代id数组,仓库名称,当前重量,数量)
        $attr = $handle->startCalculate($attr, $areaName, $weight, $num);
        if (!empty($attr)) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($attr);
        } else {
            $responseObj->setErrCode($handle->getErrCode());
            $responseObj->setErrMsg($handle->getErrMsg());
        }
    }
}